clear
set graphics off
set seed [withheld]

local samplelist $samplelist
foreach sample in `samplelist'{
	if("`sample'"=="young${routine}" | "`sample'"=="youngold${routine}" | "`sample'"=="old${routine}"){
		local agegroup "youngold${routine}"
		}
	else{
		local agegroup "`sample'"
		}
	
foreach ltfpvar in tfpvar tfpvar{
	quietly use "${data}/data_`agegroup'.dta", clear
	
	if("`sample'"=="young${routine}"){
		keep if age<=5
		}
	if("`sample'"=="old${routine}"){
		keep if age>5
		}

	if("`ltfpvar'"!="lA"){
		cap drop lA
		}
	quietly cap rename `ltfpvar' lA
			
	*Groups
	quietly egen JT		= group(J T)
	quietly egen ST		= group(S T)
	quietly egen SJT	= group(S J T)
	quietly egen tag_SJT	= tag(SJT)
	quietly egen tag_ST	= tag(ST)
	
	*Counts of groups
	
	*Tau weights * 
	quietly bys SJT	: egen emp_SJT	= total(empl*(lA!=.))
	quietly bys ST	: egen emp_ST	= total(empl*(lA!=.))
	quietly gen empshare_SJT		= emp_SJT/emp_ST
	
	*Number of Cells and Avg Plants per Cell
	quietly gen N_total 			= _N
	quietly egen N_ST 				= total(tag_ST)
	quietly egen N_SJT				= total(tag_SJT)
	quietly gen plants_percell_capT	= N_total / N_ST
	quietly gen plants_percell_tau	= N_total / N_SJT
	
	
	*Generate Raw location effects
	
		*Taus
		quietly reghdfe lA [aw=empl], absorb(temp_tau=SJT) keepsingletons
		quietly reghdfe temp_tau  if tag_SJT, absorb(JT) resid keepsingletons
		quietly predict tau if e(sample), resid	
			
		*CapT
		quietly reghdfe tau [aw=empshare_SJT], absorb(capT_temp=ST)  keepsingletons
		quietly reghdfe capT_temp if tag_ST, absorb(T) resid keepsingletons
		quietly predict capT if e(sample), resid

		*Calculate variances, save var and N, verify mean==0
		quietly tabstat capT if tag_ST, stat(N var mean) save
		matrix stats		= r(StatTotal)
		quietly gen N_capT	= stats[1,1]
		quietly gen var_capT= stats[2,1]
		
		quietly tabstat  tau [aw=empshare_SJT]  if tag_SJT, stat(N var mean) save
		matrix stats		= r(StatTotal)
		quietly gen N_tau	= stats[1,1]
		quietly gen var_tau	= stats[2,1]	
	

		foreach var in  tau capT {
			tempvar weight
			if("`var'"=="tau"){
				gen `weight'=empshare_SJT
				local tag SJT
				}
			else{
			gen `weight'=1
			local tag ST
			}
		
		
		_pctile `var' [aw=`weight'],  p(5, 15, 85, 95)
		local p5 =r(r1)
		local p15=r(r2)
		local p85=r(r3)
		local p95=r(r4)
		tempvar `var'_p90 `var'_p10
		quietly sum `var' [aw=`weight'] if `var'<`p95' & `var'>=`p85' & tag_`tag'
		quietly gen `var'_p90=r(mean)
		quietly sum `var' [aw=`weight'] if `var'<`p15' & `var'<=`p5' & tag_`tag'
		quietly gen `var'_p10=r(mean)
		quietly gen `var'_p9010=`var'_p90-`var'_p10
		}
		rename capT capTtemp
		rename tau tautemp
		bys S T: 	egen capT	= max(capTtemp)
		bys S J T: 	egen tau	= max(tautemp)
		
		if("`sample'"=="all" | "`sample'"=="young" | "`sample'"=="old"){
		sum capT  if tag_ST,d
		local A=r(p5)
		local B=r(p95)
		twoway (kdensity capT  if tag_ST, range(`A' `B') n(25)), ytitle("Density") xtitle("value")
		graph save "${results}/temp/kden_temp_capT_`ltfpvar'_`sample'${date}.gph", replace
			
		sum tau if tag_SJT,d
		local A=r(p5)
		local B=r(p95)
		twoway (kdensity tau [aw=empshare_SJT] if tag_SJT, range(`A' `B') n(25)), ytitle("Density") xtitle("value")
		graph save "${results}/temp/kden_temp_tau_`ltfpvar'_`sample'${date}.gph", replace
		}		
		quietly keep if _n==1
		quietly keep var* *_p9010 N_total N_ST N_SJT 
		quietly gen sample="`sample'"
		format var* %9.4g		


gen tfpvar="`ltfpvar'"
if ("`sample'"=="all${routine}"){
	save "${results}/temp/results${date}${routine}.dta", replace
	}
	else{
	quietly append using "${results}/temp/results${date}${routine}.dta"
	quietly save "${results}/temp/results${date}${routine}.dta", replace
	}
 
 }
 }
